﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Data;
namespace Services
{

    public partial class Student_FeeTimeService
    {
       readonly IStudent_FeeTimeEntity entity;

       public Student_FeeTimeService()
       {
           this.entity = new Student_FeeTimeEntity();

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Student_FeeTime)_model, Table.Student_FeeTime.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Student_FeeTime)_model, Table.Student_FeeTime.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Student_FeeTime GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Student_FeeTime.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Student_FeeTime> GetList()
       {
           try
           {
               return entity.GetAll(Table.Student_FeeTime.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Student_FeeTime> GetListByLINQ(Func<CoreData.Student_FeeTime, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Student_FeeTime.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Student_FeeTime GetOneByLINQ(Func<CoreData.Student_FeeTime, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Student_FeeTime.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Student_FeeTime> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Student_FeeTime.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Student_FeeTime)_model, Table.Student_FeeTime.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method

        public Student_FeeTime GetByStudentID(long StudentID)
       {
           try
           {
               return entity.GetBySearchString(" StudentID=" + StudentID, Table.Student_FeeTime.ToString()).FirstOrDefault();
           }
           catch { return null; }
        }


        public Student_FeeTime GetStudentFeeTimeByStudentIDAndType(long studentid, int type)
        {
            try
            {
                var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetStudentFeeTimeByStudentIDANDType, studentid, type)).FirstOrDefault();
                return data;
            }
            catch { return null; }
        }


        /// <summary>
        /// Lất thời hạn đóng bằng mã học viên và loại
        /// </summary>
        /// <param name="studentid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataTable GetStudentFeeTimeByStudentIDAndTypeReturnDatatable(long studentid, int type)
        {
            try
            {
                var data = entity.GetByCusTomSQLReturnDatatable(String.Format(SQLCommand.GetStudentFeeTimeByStudentIDANDType, studentid, type));
                return data;
            }
            catch { return null; }
        }

        #endregion 
    
    }
         
}
